<?php

namespace App\Http\Admin\Controller\Auth;

use App\Http\Admin\CurrentUser;
use App\Http\Admin\Middleware\AuthMiddleware;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Request\auth\AdminRequest;
use App\Request\auth\Service\Auth\AdminService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Annotation\Scene;

#[Middlewares([
    AuthMiddleware::class,
])]
#[Controller(prefix: '/adminapi/auth/admin/')]
final class AdminController extends AbstractController
{

    public function __construct
    (
        protected AdminService         $service,
        protected readonly CurrentUser $currentUser

    )
    {
    }

    #[GetMapping("lists")]
    public function lists(RequestInterface $request): Result
    {
        return $this->success($this->service->page(
            $request->all(),
            $this->getCurrentPage(),
            $this->getPageSize()
        ));
    }

    #[Scene(scene: 'updated',argument: 'request')]
    #[PutMapping('update')]
    public function update(AdminRequest $request): Result
    {
        $this->service->updateById($request->input('id'), $request->all());
        return $this->success([], '更新成功');
    }

    #[DeleteMapping('delete')]
    public function delete(RequestInterface $request): Result
    {
        $this->service->deleteById($request->input('id'));
        return $this->success([], '删除成功');
    }

    #[GetMapping('mySelf')]
    public function mySelf(): Result
    {
        $userId = $this->currentUser->getId();
        $data = $this->service->mySelf($userId);
        return $this->success($data);
    }

    #[PostMapping('logout')]
    public function logout(): Result
    {
//        $this->auth->logout();
        return $this->success([], '退出成功');
    }

    #[PutMapping('editCurrentAdmin')]
    public function editCurrentAdmin(RequestInterface $request): Result
    {
        $this->service->editCurrentAdmin($this->currentUser->getId(), $request->all());
        return $this->success([], '更新成功请重新登录');
    }


}